package controllers

import (
	"encoding/json"
	"ssims/models"
	"ssims/pkg/app"
	"ssims/pkg/e"
	"ssims/services"
	"time"

	"github.com/gin-gonic/gin"
	"github.com/unknwon/com"
)

func GetStudent(c *gin.Context) {
	page := -1
	if arg := c.Query("page"); arg != "" {
		page = com.StrTo(arg).MustInt()
	}
	limit := -1
	if arg := c.Query("limit"); arg != "" {
		limit = com.StrTo(arg).MustInt()
	}
	searchNo := ""
	if arg := c.Query("searchName"); arg != "" {
		searchNo = arg
	}
	supplierParam := map[string]interface{}{
		"page":     page,
		"limit":    limit,
		"searchNo": searchNo,
	}
	err, info, total := services.GetAllStudent(supplierParam)
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, map[string]interface{}{"value": info, "total": total}, "查询成功")
}
func GetNumbers(c *gin.Context){
	page := -1
	if arg := c.Query("page"); arg != "" {
		page = com.StrTo(arg).MustInt()
	}
	limit := -1
	if arg := c.Query("limit"); arg != "" {
		limit = com.StrTo(arg).MustInt()
	}
	supplierParam := map[string]interface{}{
		"page":     page,
		"limit":    limit,
	}
	err, info, total := services.GetNumbers(supplierParam)
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, map[string]interface{}{"value": info, "total": total}, "查询成功")
}
func GetNumbera(c *gin.Context){
	page := -1
	if arg := c.Query("page"); arg != "" {
		page = com.StrTo(arg).MustInt()
	}
	limit := -1
	if arg := c.Query("limit"); arg != "" {
		limit = com.StrTo(arg).MustInt()
	}
	supplierParam := map[string]interface{}{
		"page":     page,
		"limit":    limit,
	}
	err, info, total := services.GetNumbera(supplierParam)
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, map[string]interface{}{"value": info, "total": total}, "查询成功")
}
func GetNumberp(c *gin.Context){
	page := -1
	if arg := c.Query("page"); arg != "" {
		page = com.StrTo(arg).MustInt()
	}
	limit := -1
	if arg := c.Query("limit"); arg != "" {
		limit = com.StrTo(arg).MustInt()
	}
	supplierParam := map[string]interface{}{
		"page":     page,
		"limit":    limit,
	}
	err, info, total := services.GetNumberp(supplierParam)
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, map[string]interface{}{"value": info, "total": total}, "查询成功")
}
func DelStudent(c *gin.Context) {
	id := ""
	if arg := c.Query("student_no"); arg != "" {
		id = arg
	}
	if id == "" {
		app.INFO(c, 30001, "参数错误")
		return
	}
	err := services.DelStudent(id)
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, map[string]interface{}{}, "删除成功")
}

func UpdateStudent(c *gin.Context) {
	b, _ := c.GetRawData()
	var m map[string]string
	_ = json.Unmarshal(b, &m)
	if m["StudentNo"] == "" {
		app.INFO(c, 30000, "参数非法")
		return
	}
	StudentNo := m["StudentNo"]
	StudentName := m["StudentName"]
	StudentAge := m["StudentAge"]
	StudentSex := m["StudentSex"]
	StudentArea := m["StudentArea"]
	StudentTime := com.StrTo(m["StudentTime"]).MustInt()
	PoliticalLandscape := m["PoliticalLandscape"]
	StudentNational := m["StudentNational"]
	DeptName := m["DeptName"]
	err := services.UpdateStudent(models.Student{StudentNo: StudentNo, StudentName: StudentName, StudentSex: StudentSex, StudentArea: StudentArea, StudentAge: StudentAge, StudentTime: StudentTime, PoliticalLandscape: PoliticalLandscape, StudentNational: StudentNational, Isdel: false, DeptName: DeptName, UpdateTime: time.Now()})
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, map[string]interface{}{}, "更新成功")
}

func AddStudent(c *gin.Context) {
	b, _ := c.GetRawData()
	var m map[string]string
	_ = json.Unmarshal(b, &m)
	StudentNo := m["StudentNo"]
	StudentName := m["StudentName"]
	StudentAge := m["StudentAge"]
	StudentSex := m["StudentSex"]
	StudentArea := m["StudentArea"]
	StudentTime := com.StrTo(m["StudentTime"]).MustInt()
	PoliticalLandscape := m["PoliticalLandscape"]
	StudentNational := m["StudentNational"]
	DeptName := m["DeptName"]
	err := services.AddStudent(models.Student{StudentNo: StudentNo, StudentName: StudentName, StudentSex: StudentSex, StudentArea: StudentArea, StudentAge: StudentAge, StudentTime: StudentTime, PoliticalLandscape: PoliticalLandscape, StudentNational: StudentNational, Isdel: false, DeptName: DeptName, UpdateTime: time.Now()})
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, map[string]interface{}{}, "添加成功")
}
